<?

if(!defined(SCOT_TAG)) {
	define('SCOT_TAG', true);
	
	class Tag {
		var $tagid = 0;
		var $aFreq = 0;
		var $cFreq = 0;
		var $rFreq = 0;
		var $tagName = null;
		var $cooccurrenceList = null;
		var $date = null;
		var $narrowerTagList = null;
		var $broaderTagList = null;
		
		function getTagID() {
			return $this->tagid;
		}
		
		function Tag($tagid, $tagname, $date) {
			if(SCOT_DEBUG) echo('Call : Tag->Tag()<br>');
			
			$this->tagid = $tagid;
			$this->aFreq = 1;
			$this->tagName = $tagname;
			$this->cooccurrenceList = array();
			$this->date = $date;
			$this->narrowerTagList = array();
			$this->broaderTagList = array();
		}
		
		function getAFrequency() {
			if(SCOT_DEBUG) echo('Call : Tag->getAFrequency()<br>');
			
			return $this->aFreq;
		}
		
		function setAFrequency($freq) {
			if(SCOT_DEBUG) echo('Call : Tag->setAFrequency()<br>');
			
			$this->aFreq = $freq;
		}

		function getCFrequency() {
			if(SCOT_DEBUG) echo('Call : Tag->getAFrequency()<br>');
			
			return $this->cFreq;
		}
		
		function setCFrequency($freq) {
			if(SCOT_DEBUG) echo('Call : Tag->setAFrequency()<br>');
			
			$this->cFreq = $freq;
		}

		function getRFrequency($total) {
			return ($this->aFreq / $total * 100);
		}

		
		function addCooccurrence($cooccurrence) {
			$this->cooccurrenceList[] = $cooccurrence;
		}
		
		function getCooccurrenceList() {
			return $this->cooccurrenceList;
		}
		
		function getName() {
			$str = "";
			for($i = 0 ; $i < strlen($this->tagName) ; $i++) {
				if($this->isValiedChar($this->tagName[$i])) {
					$str .= $this->tagName[$i];
				}
			}
			
			return $str;
		}
		
		function isValiedChar($c) {
			if($c >= 'a' && $c <= 'z') {
				return true;
			} else if($c >= 'A' && $c <= 'Z') {
				return true;
			} else if($c >= '0' && $c <= '9') {
				return true;
			} else {
				return false;
			}
		}
		
		function getTagToID() {
			return str_replace(' ', '', (strtolower($this->getName())));
		}
		
		function getLastUsedDate() {
			return $this->date;
		}
		
		function addnarrowerTag($tag) {
/*			if(!in_array($tag, $this->narrowerTagList)) {
				$this->narrowerTagList[] = $tag;
			}*/
			if(!in_array($tag->getTagID(), $this->narrowerTagList)) {
				$this->narrowerTagList[] = $tag->getTagID();
//				$this->findNarrowerTag($tag);
			}
		}
		
		function addbroaderTag($tag) {
			if(!in_array($tag->getTagID(), $this->broaderTagList)) {
				$this->broaderTagList[] = $tag->getTagID();
//				$this->findBroaderTag($tag);
			}
		}
		
		function findBroaderTag($tag) {
			$return = null;
			for($i = 0 ; $i < count($this->broaderTagList) ; $i++) {
				if($this->broaderTagList[$i] == $tag) {
					$this->broaderTagList[$i] = null;
					return $this;
				} else {
					$return = $this->broaderTagList[$i]->findBroaderTag($tag);
				}
			}
			return $return;
		}
		
		function findNarrowerTag($tag) {
			$return = null;
			for($i = 0 ; $i < count($this->narrowerTagList) ; $i++) {
				if($this->narrowerTagList[$i] == $tag) {
					$this->narrowerTagList[$i] = null;
					return $this;
				} else {
					$return = $this->narrowerTagList[$i]->findNarrowerTag($tag);
				}
			}
			return $return;
		}
		
		function getNarrowerTagList() {
			return $this->narrowerTagList;
		}
		
		function getBroaderTagList() {
			return $this->broaderTagList;
		}
	}
}
?>